const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController');
const { authenticate } = require('../middleware/authMiddleware');

// 添加路由日志中间件
router.use((req, res, next) => {
  // 移除调试日志输出
  next();
});

// 公共路由（无需认证）
router.post('/login', userController.login);
router.post('/wx/login', userController.wxLogin); // 微信登录路由
router.post('/register', userController.register);

// 邮箱验证码路由 - 通过中间件处理
router.post('/email/verify', (req, res, next) => {
  // 如果是bind类型验证码，则需要认证
  if (req.body.type === 'bind') {
    authenticate(req, res, () => {
      userController.sendEmailVerificationCode(req, res);
    });
  } else {
    // 其他类型无需认证
    userController.sendEmailVerificationCode(req, res);
  }
});

// 需要认证的路由
router.get('/profile', authenticate, userController.getUserProfile);
router.put('/profile', authenticate, userController.updateUserProfile);
router.get('/balance', authenticate, userController.getUserBalance);
router.post('/bind/wechat', authenticate, userController.bindWechatAccount);
router.post('/bind/email', authenticate, userController.bindEmailAccount);

module.exports = router; 